package com.jasonli.controller;

import com.jasonli.domain.PageBean;
import com.jasonli.domain.ProductInfo;
import com.jasonli.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/infos")
public class ProductInfoController {

    @Autowired
    private ProductInfoService infoService;

    @PostMapping
    public Result save(@RequestBody ProductInfo info) {
        boolean flag = infoService.save(info);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    @DeleteMapping("/{pid}")
    public Result delete(@PathVariable Integer pid) {
        boolean flag = infoService.delete(pid);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    @DeleteMapping
    public Result deleteByPids(@RequestBody List<Integer> pids) {
        boolean flag = infoService.deleteByPids(pids);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    @PutMapping
    public Result update(@RequestBody ProductInfo info) {
        boolean flag = infoService.update(info);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    @GetMapping("/{pid}")
    public Result getByPid(@PathVariable Integer pid) {
        ProductInfo info = infoService.getByPid(pid);
        Integer code = info != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code, info);
    }

    @GetMapping("/type/{tid}")
    public Result getByTid(@PathVariable Integer tid) {
        List<ProductInfo> infoList = infoService.getByTid(tid);
        Integer code = infoList != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code, infoList);
    }

    @GetMapping
    public Result getAll() {
        List<ProductInfo> infoList = infoService.getAll();
        Integer code = infoList != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code, infoList);
    }

    //分页查询
    @GetMapping("/page/{currentPage}/{pageSize}")
    public Result selectByPage(@PathVariable Integer currentPage,@PathVariable Integer pageSize) {
        PageBean<ProductInfo> pageBean = infoService.selectByPage(currentPage,pageSize);
        Integer code = pageBean != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code, pageBean);
    }

    //分页条件查询
    @PostMapping("/page/{currentPage}/{pageSize}")
    public Result selectByPageAndCondition(@PathVariable Integer currentPage,@PathVariable Integer pageSize,@RequestBody ProductInfo info) {
        PageBean<ProductInfo> pageBean = infoService.selectByPageAndCondition(currentPage,pageSize,info);
        Integer code = pageBean != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code, pageBean);
    }

}
